package com.partner.match.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.partner.match.common.BaseResponse;
import com.partner.match.common.ErrorCode;
import com.partner.match.common.ResultUtils;
import com.partner.match.exception.BusinessException;
import com.partner.match.model.dto.user.UserLoginRequest;
import com.partner.match.model.dto.user.UserRegisterRequest;
import com.partner.match.model.entity.User;
import com.partner.match.model.vo.LoginUserVO;
import com.partner.match.model.vo.UserVO;
import com.partner.match.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: jzz
 * @CreateTime: 2025-01-11
 * @Description: 用户接口控制器
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/search/tags")
    @ApiOperation("通过标签搜索用户")
    public BaseResponse<List<UserVO>> searchUserByTag(@RequestParam(required = false) List<String> tagNameList) {
        if (CollUtil.isEmpty(tagNameList)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<UserVO> tagsList = userService.getUserTagsList(tagNameList);
        return ResultUtils.success(tagsList);
    }


    @GetMapping("/recommend")
    @ApiOperation("获取全部用户")
    public BaseResponse<Page<User>> recommendUsers(long currentPage, long pageSize, HttpServletRequest request) {
        Page<User> tagsList = userService.recommendUsers(currentPage, pageSize, request);
        return ResultUtils.success(tagsList);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public BaseResponse<Long> registerUser(@RequestBody UserRegisterRequest userRegisterRequest) {
        String userName = userRegisterRequest.getUserName();
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String tags = userRegisterRequest.getTags();
        Long userId = userService.registerUser(userName, userPassword, userAccount, checkPassword,tags);
        return ResultUtils.success(userId);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public BaseResponse<LoginUserVO> login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();

        LoginUserVO loginUserVO = userService.login(userAccount, userPassword, request);

        return ResultUtils.success(loginUserVO);
    }

    @GetMapping("/exit")
    @ApiOperation("退出登录")
    public BaseResponse<Boolean> exitLogin(){
        StpUtil.logout();
        return ResultUtils.success(true);
    }


    @GetMapping("/current")
    @ApiOperation("获取当前登录用户")
    public BaseResponse<LoginUserVO> getCurrentUser(HttpServletRequest request) {
        LoginUserVO userVO = userService.getCurrentUser(request);
        return ResultUtils.success(userVO);
    }

    @PostMapping("/update")
    @ApiOperation("修改用户信息")
    public BaseResponse<Boolean> updateUser(@RequestBody User user, HttpServletRequest request) {
        // 判空
        if (user == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 在service中做权限校验和修改信息
        Boolean updateFlag = userService.updateUser(user, request);
        return ResultUtils.success(updateFlag);
    }

    @GetMapping("/match")
    @ApiOperation("获取匹配的用户")
    public BaseResponse<List<UserVO>> matchUsers(Long num) {
        if (num <= 0 || num > 20) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<UserVO> userVOList = userService.matchUsers(num);
        return ResultUtils.success(userVOList);
    }
}
